package com.itany.corejava.code13_jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author MissChen
 * @date 2024年03月22日 10:51
 */
public class Test07_多表保存 {
    public static void main(String[] args) throws DataAccessException {
        // Dept dept = new Dept("工程部", "管理公司大大小小的工程业务");
        // Integer id = insertReturnPrimaryKey(dept);
        // Emp emp = new Emp("alice", 8000.0, id);
        // Emp emp2 = new Emp("alice2", 5000.0, id);
        // Emp emp3 = new Emp("alice3", 7000.0, id);
        // insertEmp(emp);
        // insertEmp(emp2);
        // insertEmp(emp3);

        Dept dept = new Dept("营销部", "营销公司的整体销售业务");
        Integer id = insertReturnPrimaryKey(dept);
        dept.setId(id);
        Emp e=new Emp("tom",7800.0,dept);
        Emp e2=new Emp("huahua",9000.0,dept);
        Emp e3=new Emp("coco",8000.0,dept);
        insertEmp(e);
        insertEmp(e2);
        insertEmp(e3);




    }

    public static Integer insertReturnPrimaryKey(Dept dept) throws DataAccessException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Integer id = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = new StringBuffer()
                    .append(" insert into t_dept ")
                    .append(" (name,remark) ")
                    .append("  values ")
                    .append(" (?,?) ")
                    .toString();

            ps = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
            ps.setString(1, dept.getName());
            ps.setString(2, dept.getRemark());
            ps.executeUpdate();
            rs = ps.getGeneratedKeys();

            while (rs.next()) {
                id = rs.getInt(1);
            }
            return id;
        } catch (DataAccessException e) {
            throw e;
        } catch (SQLException e) {
            throw new DataAccessException("数据库访问异常");
        } finally {
            JDBCUtil.close(conn, ps, rs);
        }
    }

    public static void insertEmp(Emp emp) throws DataAccessException {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = new StringBuffer()
                    .append(" insert into t_emp ")
                    .append(" (name,salary,dept_id) ")
                    .append("  values ")
                    .append(" (?,?,?) ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1, emp.getName());
            ps.setDouble(2, emp.getSalary());

            //使用外键字段关联
            // ps.setInt(3,emp.getDeptId());

            //使用对象属性关联
            ps.setInt(3, emp.getDept().getId());
            ps.executeUpdate();
        } catch (DataAccessException e) {
            throw e;
        } catch (SQLException e) {
            throw new DataAccessException("数据库访问异常");
        } finally {
            JDBCUtil.close(conn, ps, null);
        }

    }


}
